package com.k.demo.websocket;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@ServerEndpoint(value = "/wss/cservice/{rule}/{name}", configurator = MyWSConfig.class)
@Component
public class CustomerServiceServer {
	
	static Map<String,WSValue> SESSION_MAP = new HashMap<String,WSValue>();
	
	static Session getSessionByRule(int rule) {	
		for(String key : SESSION_MAP.keySet()) {
			WSValue value = SESSION_MAP.get(key);
			if(value.getRule()==rule) {
				return value.getSession();
			}
		}
		return null;
	}
	
	static Session getSessionByName(String name) {	
		for(String key : SESSION_MAP.keySet()) {
			WSValue value = SESSION_MAP.get(key);
			if(value.getName().equals(name)) {
				return value.getSession();
			}
		}
		return null;
	}
	
	//这个方法会在建立连接的时候自动调用
	@OnOpen
	public void open(@PathParam("rule") int rule,@PathParam("name") String name, Session session) {
		System.out.println("open");
		
		String clientKey = (String) session.getUserProperties().get("clientKey");
		SESSION_MAP.put(clientKey,new WSValue(session, name, rule));
		
		System.out.println("有新的连接, 总数:" + SESSION_MAP.size());
		System.out.println(SESSION_MAP);
		
		//给客户端发送信息
		try {
			session.getBasicRemote().sendText("hello "+name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//关闭连接的时候被调用
	@OnClose
	public void close(Session session) {
		SESSION_MAP.remove((String) session.getUserProperties().get("clientKey"));
		System.out.println("close");
	}
	
	
	//发送信息的时候,自动调用
	@OnMessage
	public void message(String msg,Session session) {
		System.out.println("OnMessage");
		System.out.println(msg);
		String clientKey = (String)session.getUserProperties().get("clientKey");
		WSValue wsValue = SESSION_MAP.get(clientKey);
		String fromUserName = wsValue.getName();
		
		JSONObject jsonObject = JSON.parseObject(msg);
		String toUserName = jsonObject.getString("toUser");
		String textMessage = jsonObject.getString("textMessage");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fromUser", fromUserName);
		map.put("textMessage", textMessage);
		
		System.out.println(map);
		
		//0表示顾客，1表示客服
		if (wsValue.getRule() == 0) {
			try {
				getSessionByRule(1).getBasicRemote().sendText(JSON.toJSONString(map));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			try {
				getSessionByName(toUserName).getBasicRemote().sendText(JSON.toJSONString(map));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//发生错误的时候,自动调用
	@OnError
	public void error(Session session, Throwable error) {
		System.out.println("error");
	}
	
}
